package com.sxau.software_educational_system_backend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxau.software_educational_system_backend.annotation.AdminOneOrTwo;
import com.sxau.software_educational_system_backend.annotation.AdminOnlyOne;
import com.sxau.software_educational_system_backend.annotation.AdminOnlyTwo;
import com.sxau.software_educational_system_backend.common.BaseResponse;
import com.sxau.software_educational_system_backend.common.ResultUtils;
import com.sxau.software_educational_system_backend.domain.entity.Content;
import com.sxau.software_educational_system_backend.service.ContentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/contents")
@RequiredArgsConstructor
@Api(tags = "文章信息管理接口")
public class ContentController {
    private final ContentService contentService;

    @GetMapping("/types/{type}")
    @ApiOperation("分页查询文章信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "文章类型", required = true),
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", defaultValue = "10")
    })
    public BaseResponse<Page<Content>> getPage(
            @PathVariable String type,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<Content> contentPage = contentService.pageByType(new Page<>(page, pageSize), type);
        return ResultUtils.success(contentPage);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据ID查询文章详情")
    public BaseResponse<Content> getById(@PathVariable Integer id) {
        return ResultUtils.success(contentService.getById(id));
    }

    @GetMapping("/all/{type}")
    @ApiOperation("获取所有文章列表")
    public BaseResponse<List<Content>> getAllByType(@PathVariable String type) {
        return ResultUtils.success(contentService.getByType(type));
    }

    @DeleteMapping("/yz/{id}")
    @ApiOperation("删除文章信息")
    @AdminOneOrTwo
    public BaseResponse<Void> delete(@PathVariable Integer id) {
        contentService.removeById(id);
        return ResultUtils.success();
    }

    @PostMapping("/yz")
    @ApiOperation("新增文章信息")
    @AdminOneOrTwo
    public BaseResponse<Content> create(@RequestBody Content content) {
        contentService.save(content);
        return ResultUtils.success(content);
    }

    @PutMapping("/yz/{id}")
    @ApiOperation("更新文章信息")
    @AdminOneOrTwo
    public BaseResponse<Content> update(@PathVariable Integer id,@RequestBody Content content) {
        content.setId(id);
        contentService.updateById(content);
        return ResultUtils.success(contentService.getById(id));
    }
}
